package com.zkca.xf.ui.activity.baidumap;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseTitleActivity;
import com.zkca.xf.model.javabean.BaiDuRouteBean;
import com.zkca.xf.presenter.BaiDuRoutePresenter;
import com.zkca.xf.view.BaiDuRouteView;


import java.util.HashMap;
import java.util.Map;

//百度线路规划
public class BaiDuRoutePlanningActivity extends BaseTitleActivity implements OnGetRoutePlanResultListener, BaiDuRouteView {
    private MapView mMapView = null;
    BaiduMap mBaidumap = null;
    RoutePlanSearch mSearch = null;    // 搜索模块，也可去掉地图模块独立使用
    OverlayManager routeOverlay = null;
    private BaiDuRoutePresenter baiDuRoutePresenter;
    private String jingdu,weidu,id;
    private Map<String,String> map;
    private LatLng point;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bai_du_route_planning);
        baiDuRoutePresenter=new BaiDuRoutePresenter(this,this);
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaidumap = mMapView.getMap();
        mSearch = RoutePlanSearch.newInstance();
        Intent intent = getIntent();
        jingdu = intent.getStringExtra("jingdu");
        weidu = intent.getStringExtra("weidu");
        id = intent.getStringExtra("id");
        //设置假数据
        point = new LatLng(Double.valueOf(weidu), Double.valueOf(jingdu));
        initview();
        initDate();
        mSearch.setOnGetRoutePlanResultListener(this);
    }

    private void initDate() {
        map = new HashMap();
        map.put("id",id);
        baiDuRoutePresenter.BaiDuRoute(map);
    }


    private void initview() {
        setTitleText("路线规划");
        setleftTextVisibility(true);
        setleftImgVisibility(true);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

    @Override
    public void onGetWalkingRouteResult(WalkingRouteResult result) {

    }

    @Override
    public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {

    }

    @Override
    public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

    }

    @Override
    public void onGetDrivingRouteResult(DrivingRouteResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(BaiDuRoutePlanningActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
        }
        if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
            // 起终点或途经点地址有岐义，通过以下接口获取建议查询信息
            // result.getSuggestAddrInfo()
            return;
        }

         if (result.getRouteLines().size() >= 1) {
             DrivingRouteOverlay overlay = new MyDrivingRouteOverlay(mBaidumap);
             routeOverlay = overlay;
             mBaidumap.setOnMarkerClickListener(overlay);
             overlay.setData(result.getRouteLines().get(0));
             overlay.addToMap();
             overlay.zoomToSpan();
            } else {
                Log.d("route result", "结果数<0");
                return;
            }
    }

    @Override
    public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

    }

    @Override
    public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {

    }



    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getBaiDuRoute(BaiDuRouteBean dataBean) {
        PlanNode stNode = PlanNode.withLocation(new LatLng(Double.valueOf(dataBean.getStart_lat()), Double.valueOf(dataBean.getEnd_lon())));
        PlanNode enNode = PlanNode.withLocation(point);
        mSearch.drivingSearch((new DrivingRoutePlanOption())
                .from(stNode)
                .to(enNode));
    }


    // 定制RouteOverly
    private class MyDrivingRouteOverlay extends DrivingRouteOverlay {

        public MyDrivingRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public BitmapDescriptor getStartMarker() {
                return BitmapDescriptorFactory.fromResource(R.mipmap.icon_st);
        }

        @Override
        public BitmapDescriptor getTerminalMarker() {
                return BitmapDescriptorFactory.fromResource(R.mipmap.icon_en);
        }
    }
}
